package com.wuxiaofan.api;

import com.alibaba.fastjson.JSONObject;
import com.wuxiaofan.entity.UserInfo;
import com.wuxiaofan.service.UserInfoService;
import com.wuxiaofan.vo.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;

@RestController
@RequestMapping("userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    @RequestMapping("subBalance")
    public ReturnResult subBalance(@RequestBody JSONObject jsonObject) throws Exception{
        String id = jsonObject.getString("id");
        BigDecimal gold = jsonObject.getBigDecimal("gold");
        UserInfo userInfo = userInfoService.findById(id);
        if(userInfo.getBalance().compareTo(gold)!=-1){
            HashMap paraMap = new HashMap();
            paraMap.put("id",id);
            paraMap.put("balance",userInfo.getBalance().subtract(gold));
            userInfoService.updateByMap(paraMap);
        }else{
            throw new Exception("余额不足");
        }
        return ReturnResult.success("减余额成功");
    }
}
